﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Microsoft.Win32;

namespace Downloader
{
    class IconUtil
    {
        /// <summary>   
        /// 通过扩展名得到图标和描述   
        /// </summary>   
        /// <param name="ext">扩展名</param>   
        /// <param name="LargeIcon">得到大图标</param>   
        /// <param name="smallIcon">得到小图标</param>   
        public static void GetExtsIconAndDescription(string ext, out Icon largeIcon, out Icon smallIcon, out string description)
        {
            largeIcon = smallIcon = null;
            description = "";
            var extsubkey = Registry.ClassesRoot.OpenSubKey(ext);   //从注册表中读取扩展名相应的子键   
            if (extsubkey != null)
            {
                var extdefaultvalue = (string)extsubkey.GetValue(null);     //取出扩展名对应的文件类型名称   
                if (extdefaultvalue == null)
                {
                    return;
                }
                var typesubkey = Registry.ClassesRoot.OpenSubKey(extdefaultvalue);  //从注册表中读取文件类型名称的相应子键   
                if (typesubkey != null)
                {
                    description = (string)typesubkey.GetValue(null);   //得到类型描述字符串   
                    var defaulticonsubkey = typesubkey.OpenSubKey("DefaultIcon");  //取默认图标子键   
                    if (defaulticonsubkey != null)
                    {
                        //得到图标来源字符串   
                        var defaulticon = (string)defaulticonsubkey.GetValue(null); //取出默认图标来源字符串   
                        var iconstringArray = defaulticon.Split(',');
                        int nIconIndex = 0;
                        if (iconstringArray.Length > 1) int.TryParse(iconstringArray[1], out nIconIndex);
                        //得到图标   
                        System.IntPtr phiconLarge = new IntPtr();
                        System.IntPtr phiconSmall = new IntPtr();
                        NativeMethods.ExtractIconExW(iconstringArray[0].Trim('"'), nIconIndex, ref phiconLarge, ref phiconSmall, 1);
                        if (phiconLarge.ToInt32() > 0) largeIcon = Icon.FromHandle(phiconLarge);
                        if (phiconSmall.ToInt32() > 0) smallIcon = Icon.FromHandle(phiconSmall);
                    }
                }
            }
        }
        //----以下是Pinvoke生成代码----   

        [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
        public struct HICON__
        {

            /// int   
            public int unused;
        }

        public partial class NativeMethods
        {

            /// Return Type: UINT->unsigned int   
            ///lpszFile: LPCWSTR->WCHAR*   
            ///nIconIndex: int   
            ///phiconLarge: HICON*   
            ///phiconSmall: HICON*   
            ///nIcons: UINT->unsigned int   
            [System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint = "ExtractIconExW", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
            public static extern uint ExtractIconExW([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string lpszFile, int nIconIndex, ref System.IntPtr phiconLarge, ref System.IntPtr phiconSmall, uint nIcons);

        }  
    }
}
